package com.carbonspark.ecoquest.application.controller;

import com.alibaba.fastjson.JSON;
import com.carbonspark.auth.api.UserFeignService;

import com.carbonspark.auth.entity.AuthUserDTO;
import com.carbonspark.auth.entity.Result;
import com.carbonspark.ecoquest.application.convert.TaskDTOConverter;
import com.carbonspark.ecoquest.application.convert.TaskRulesDTOConverter;
import com.carbonspark.ecoquest.application.convert.TaskUserDTOConverter;
import com.carbonspark.ecoquest.application.dto.TaskDTO;
import com.carbonspark.ecoquest.application.dto.TaskRulesDTO;
import com.carbonspark.ecoquest.application.dto.TaskUserDTO;
import com.carbonspark.ecoquest.common.entity.PageResult;


import com.carbonspark.ecoquest.common.utils.LoginUtil;
import com.carbonspark.ecoquest.domain.entity.TaskBO;
import com.carbonspark.ecoquest.domain.entity.TaskRulesBO;
import com.carbonspark.ecoquest.domain.entity.TaskUserBO;
import com.carbonspark.ecoquest.domain.service.TaskDomainService;
import com.carbonspark.ecoquest.domain.service.TaskRulesDomainService;
import com.carbonspark.ecoquest.domain.service.TaskUserDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务接口
 */

@RestController
@RequestMapping("/task")
@Slf4j
public class TaskController {
    @Resource
    private TaskDomainService taskDomainService;
    @Resource
    private TaskRulesDomainService taskRulesDomainService;
    @Resource
    private TaskUserDomainService taskUserDomainService;
    @Resource
    private UserFeignService userFeignService;

    /**
     * 获取任务详情
     * @param taskDTO
     * @return
     */
   @PostMapping("getTask")
    public Result<TaskDTO> getUserInfo(@RequestBody TaskDTO taskDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskController.getTask.dto:{}", JSON.toJSONString(taskDTO));
            }
            Preconditions.checkNotNull(taskDTO.getId(),"任务id不能为空");
            TaskBO taskBO = TaskDTOConverter.INSTANCE.convertDTOToBO(taskDTO);
            TaskBO taskDetailBO =  taskDomainService.queryById(taskBO);
            TaskDTO taskDTOResult = TaskDTOConverter.INSTANCE.convertBOToDTO(taskDetailBO);
            String loginId = LoginUtil.getLoginId();
            AuthUserDTO userDTO = new AuthUserDTO();
            userDTO.setUserName(loginId);
           Result<AuthUserDTO> authUserDTOResult = userFeignService.getUserInfo(userDTO);
            //获取当前用户的id
            Long userId = authUserDTOResult.getData().getId();
            //判断是否完成
            TaskUserBO taskUserBO  = taskUserDomainService.queryIsCompleted(userId, taskDTO.getId());
            //把规则塞到任务详情里
            List<TaskRulesBO> taskRulesBOS = taskRulesDomainService.queryById(taskDTOResult.getId());
            List<TaskRulesDTO> taskRulesDTOS = TaskRulesDTOConverter.INSTANCE.convertBOToDTO(taskRulesBOS);
            taskDTOResult.setTaskDTOList(taskRulesDTOS);

            //如果为空则是还没有接受当前这个页面的任务
            if (taskUserBO!=null){
                TaskUserDTO taskUserDTO = TaskUserDTOConverter.INSTANCE.convertBOToDTO(taskUserBO);
                //是否完成的值
                //如果为
                Integer isCompleted = taskUserDTO.getIsCompleted();


                taskDTOResult.setIsCompleted(isCompleted);

                return Result.ok(taskDTOResult);
            }
           return Result.ok(taskDTOResult);
        } catch (Exception e) {
            log.error("TaskController.getTask.error:{}", e.getMessage(), e);
            return Result.fail("获取任务详情失败");
        }
    }
    /**
     * 查询所有任务
     * @param taskDTO
     * @return
     */
    @PostMapping("getTaskList")
    public Result<PageResult<TaskDTO>> getTaskList(@RequestBody TaskDTO taskDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskController.getTaskList.dto:{}", JSON.toJSONString(taskDTO));
            }

            TaskBO taskBO = TaskDTOConverter.INSTANCE.convertDTOToBO(taskDTO);
            taskBO.setPageNo(taskDTO.getPageNo());
            taskBO.setPageSize(taskDTO.getPageSize());
            PageResult<TaskBO> taskDetailBO =  taskDomainService.selectTaskList(taskBO);

            return Result.ok(taskDetailBO);
        } catch (Exception e) {
            log.error("TaskController.getTaskList.error:{}", e.getMessage(), e);
            return Result.fail("获取任务列表失败");
        }
    }
    /**
     * 修改任务
     * @param taskDTO
     * @return
     */
    @PostMapping("updateTask")
    public Result<TaskDTO> updateTask(@RequestBody TaskDTO taskDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskController.updateTask.dto:{}", JSON.toJSONString(taskDTO));
            }
            Preconditions.checkNotNull(taskDTO.getId(),"任务id不能为空");
            TaskBO taskBO = TaskDTOConverter.INSTANCE.convertDTOToBO(taskDTO);
            TaskBO taskDetailBO =  taskDomainService.update(taskBO);
            TaskDTO result = TaskDTOConverter.INSTANCE.convertBOToDTO(taskDetailBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TaskController.updateTask.error:{}", e.getMessage(), e);
            return Result.fail("修改任务详情失败");
        }
    }
    /**
     * 删除任务
     * @param taskDTO
     * @return
     */
    @PostMapping("deleteTask")
    public Result<TaskDTO> deleteTask(@RequestBody TaskDTO taskDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskController.deleteTask.dto:{}", JSON.toJSONString(taskDTO));
            }
            Preconditions.checkNotNull(taskDTO.getId(),"任务id不能为空");
            TaskBO taskBO = TaskDTOConverter.INSTANCE.convertDTOToBO(taskDTO);
            Boolean result =  taskDomainService.deleteById(taskBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("TaskController.deleteTask.error:{}", e.getMessage(), e);
            return Result.fail("删除任务详情失败");
        }
    }

}
